package algorithm;

public class BubbleSort {
    static void sort(int[] arr) {
        for (int i = 0; i < arr.length-1; i++) {
            int flag = 0;
            for (int j = 0; j < arr.length -i-1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    flag = 1;
                }
            }
            if(flag==0){
                break;
            }
        }
    }

    static void sort2(int []arr){
        for(int i=0;i<arr.length-1;i++){
            int flag = 0;
            for(int j=0;j<arr.length-1-i;j++){
                 if(arr[j] > arr[j+1]){
                     int temp = arr[j];
                     arr[j] = arr[j+1];
                     arr[j+1] = temp;
                     flag=1;
                 }
            }
            if(flag==0){
                return;
            }
        }
    }
    public static void main(String[] args) {
        int[] arr = {70,30,40,10,80,20,90,100,75,60,45};
        sort2(arr);
        for (int i = 0; i <arr.length ; i++) {
            System.out.print(arr[i]+" ");
        }
    }
}
